package com.platform.service.impl;

import com.platform.cache.J2CacheUtils;
import com.platform.dao.ProCategoryDao;
import com.platform.dao.ProStyleDao;
import com.platform.dao.ProductCateDao;
import com.platform.dao.ProductDao;
import com.platform.entity.ProCategoryEntity;
import com.platform.entity.ProStyleEntity;
import com.platform.entity.ProductEntity;
import com.platform.service.ProductNewService;
import com.platform.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.util.stream.Collectors.toList;

/**
 * @program:
 * @Description: 编辑商品:所有商品
 * @Author: liweihai
 * @Date: Created in 2019/1/31 17:10
 */
@Service
public class ProductNewServiceImpl implements ProductNewService {
    @Autowired
    private ProductDao productDao;

    @Autowired
    private ProductCateDao tProductCateDao;

    @Autowired
    private ProStyleDao proStyleDao;

    @Autowired
    private ProCategoryDao proCategoryDao;


    @Override
    public ProductEntity queryObject(Integer id) {
        ProductEntity productEntity = productDao.queryObject(id);
        Long pid = productEntity.getId();
        Map<String, Object> map = new HashMap<>();
        map.put("pid", pid);
        //根据ID查询款式列表
        List<ProStyleEntity> proStyleEntities = proStyleDao.queryList(map);
        productEntity.setProStyles(proStyleEntities);
        return productEntity;
    }


    @Override
    public List<ProductEntity> queryList(Map<String, Object> map) {
        map.put("merchantId", 1);
        List<ProductEntity> list = productDao.queryList(map);

        return list;
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        map.put("merchantId", 1);
        return productDao.queryTotal(map);
    }

    @Override
    public int save(ProductEntity tag) {
        ProCategoryEntity categoryEntity = null;
        List<ProStyleEntity> proStyles = tag.getProStyles();
        if (tag.getCateL2() != null && tag.getCateL2() != 0) {
            categoryEntity = proCategoryDao.queryObject(tag.getCateL2());//获得利率
            tag.setGiveEnergy(categoryEntity.getEnergyRate());
            tag.setGiveTeam(categoryEntity.getGaveTeam());
            tag.setSpcFree(categoryEntity.getSpcFree());
        }
        tag.setAuditStatus(2);
        tag.setMerchantId(1);
        tag.setMerchantName("动动体育");
        String banners = proStyles.stream().map(ProStyleEntity::getImageUrl).reduce((a, b) -> a + "," + b).toString();
        if (tag.getBannerList() == null || tag.getBannerList().size() == 0) {
            tag.setBanners(banners);
        } else {
            banners = tag.getBannerList().stream().reduce((a, b) -> a + "," + b).toString();
            tag.setBanners(banners);
        }
        tag.setContent("<script>document.getElementsByTagName('meta')['viewport'][\"content\"] = \"width=375,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no\";</script><style>img{width:100%;display:block}</style>" + tag.getBody());
        int save = productDao.save(tag);
        //清理KEY="HOME_KEY"在Redis的缓存
        J2CacheUtils.clear(Constants.REDIS_HOME);


        for (ProStyleEntity proStyleEntity : proStyles) {
            proStyleEntity.setPid(tag.getId());
            if (categoryEntity != null) {
                proStyleEntity.setGiveEnergy(proStyleEntity.getPrice().multiply(categoryEntity.getEnergyRate()));
                proStyleEntity.setGiveTeam(proStyleEntity.getPrice().multiply(categoryEntity.getGaveTeam()));
                proStyleEntity.setSpcFree(proStyleEntity.getPrice().multiply(categoryEntity.getSpcFree()));
            }
            proStyleDao.save(proStyleEntity);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("cateId", tag.getCateL2());
        map.put("cateParentId", tag.getCateL1());
        map.put("productId", tag.getId());
        tProductCateDao.save(map);
        return save;
    }

    @Override
    public int update(ProductEntity tag) {
        ProCategoryEntity categoryEntity = null;
        List<ProStyleEntity> proStyles = tag.getProStyles();
        if (tag.getCateL2() != null && tag.getCateL2() != 0) {
            categoryEntity = proCategoryDao.queryObject(tag.getCateL2());//利率
            tag.setGiveEnergy(categoryEntity.getEnergyRate());
            tag.setGiveTeam(categoryEntity.getGaveTeam());
            tag.setSpcFree(categoryEntity.getSpcFree());
        }
        tag.setAuditStatus(2);
        tag.setMerchantId(1);
        tag.setMerchantName("动动体育");
        tag.setContent("<script>document.getElementsByTagName('meta')['viewport'][\"content\"] = \"width=375,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no\";</script><style>img{width:100%;display:block}</style>" + tag.getBody());
        List<String> strs = proStyles.stream().map(ProStyleEntity::getImageUrl).collect(toList());
        String banners = String.join(",", strs);
        if (tag.getBannerArr() == null || tag.getBannerArr().size() == 0) {
            tag.setBanners(banners);
        } else {
            banners = String.join(",", tag.getBannerArr());
            tag.setBanners(banners);
        }
        int update = productDao.update(tag);
        tProductCateDao.delete(tag.getId());
        Map<String, Object> map1 = new HashMap<>();
        map1.put("pid", tag.getId());
        proStyleDao.delete(map1);

        for (ProStyleEntity proStyleEntity : proStyles) {
            proStyleEntity.setPid(tag.getId());
            if (categoryEntity != null) {
                proStyleEntity.setGiveEnergy(proStyleEntity.getPrice().multiply(categoryEntity.getEnergyRate()));
                proStyleEntity.setGiveTeam(proStyleEntity.getPrice().multiply(categoryEntity.getGaveTeam()));
                proStyleEntity.setSpcFree(proStyleEntity.getPrice().multiply(categoryEntity.getSpcFree()));
            }
            proStyleDao.save(proStyleEntity);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("cateId", tag.getCateL2());
        map.put("cateParentId", tag.getCateL1());
        map.put("productId", tag.getId());
        tProductCateDao.save(map);
        //清理KEY="HOME_KEY"在Redis的缓存
        J2CacheUtils.clear(Constants.REDIS_HOME);
        return update;
    }

    @Override
    public int delete(Integer id) {
        //清理KEY="HOME_KEY"在Redis的缓存
        J2CacheUtils.clear(Constants.REDIS_HOME);
        int delete = productDao.delete(id);
        return delete;
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        //清理KEY="HOME_KEY"在Redis的缓存
        J2CacheUtils.clear(Constants.REDIS_HOME);
        int deleteBatch = productDao.deleteBatch(ids);
        Map map = new HashMap();
        for (Integer pid : ids) {
            map.put("pid", pid);
            proStyleDao.delete(map);
            tProductCateDao.delete(map);
        }
        return deleteBatch;
    }

    @Override
    public List<ProductEntity> queryObjectByMId(Integer merchantId) {
        return productDao.queryObjectByMId(merchantId);
    }


}
